import numpy as np

print('列表转为矩阵')
one_list = [1, 2, 3, 4, 5]
array = np.array(one_list)
print(type(array), array)

print('矩阵维度', array.ndim)
print('行数和列数', array.shape)
print('元素个数', array.size)
print('查看元素类型', array.dtype, array.dtype.name)
float_array = array.astype(np.float64)
print('转换数据类型', float_array.dtype)

another_list = [[1,2,3],[4,5,6]]
array = np.array(another_list)
print(type(array), array)

print('矩阵维度', array.ndim)
print('行数和列数', array.shape)
print('元素个数', array.size)

print('创建全 0 矩阵')
all_zero = np.zeros((3,4))
print(all_zero)

print('创建全 1 矩阵并指定类型')
all_one = np.ones((3, 4), dtype=np.int)
print(all_one)

print('创建连续矩阵')
serial_array = np.arange(1,12,2)
print(serial_array)

print('改变上一个矩阵形状')
shape_array = serial_array.reshape((2,3))
print(shape_array)

print('创建插值矩阵')
lin_array = np.linspace(1,20,10)
print(lin_array)

print('改变上一个矩阵形状')
shape_lin_array = lin_array.reshape((5,2))
print(shape_lin_array)

print('一维矩阵运算')
a = np.array([5, 10, 15, 20])
b = np.arange(4)
print('a', a)
print('b', b)
print('a + b', a + b)
print('a - b', a - b)
print('a * b', a * b)
print('a / b', a / b)
print('b^2', b**2)
print('sin(a)', np.sin(a))
print('b < 2', b<2)
print('a == b', a == b)

print('多维矩阵运算')
a = np.array([[1,2], [3,4]])
b = np.arange(4).reshape((2,2))
print('a', a)
print('b', b)
print('a·b', a.dot(b))
print('a·b', np.dot(a,b))
print('sum(a)', np.sum(a))
print('min(a)', np.min(a))
print('max(a)', np.max(a))
print('a 按行求和', np.sum(a, axis=1))
print('a 按列求和', np.sum(a, axis=0))
print('a 按行 max', np.max(a, axis=1))
print('a 按列 min', np.min(a, axis=0))
print('a 最小元素索引', np.argmin(a))
print('a 最大元素索引', np.argmax(a))
print('a 均值（三种方式）', np.mean(a), np.average(a), a.mean())
print('a 中位数', np.median(a))
print('a 累加', np.cumsum(a))
print('a 累差', np.diff(a))
print('固定随机数种子', np.random.seed(0))
print('随机数', np.random.rand(3, 3))
print('固定随机数种子', np.random.seed(0))
print('再次随机，一样的', np.random.rand(3, 3))